<template>

  <div>
<van-pull-refresh v-model="refreshing" @refresh="onRefresh">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      :immediate-check="false"
      offset="50"
      @load="onLoad"
    >

      <article-item
        v-for="item in articleList"
        :key="item.art_id"
        :obj="item"

         @click.native="$router.push(`/article_detail?aid=${item.art_id}`)"
      />

    </van-list>
    </van-pull-refresh>

  </div>
</template>

<script>
import { timeAgo } from '@/utils/date.js'
import { articlesListAPI } from '@/api'
import ArticleItem from './components/ArticleItem.vue'

export default {
  components: { ArticleItem },
  data () {
    return {
      articleList: [],
      loading: false,
      finished: false,
      nextTime: null,
      refreshing: false
    }
  },
  props: {
    cid: Number
  },
  async created () {
    this.getArticlesList()
  },
  methods: {
    async getArticlesList () {
      const res = await articlesListAPI({
        channelId: this.cid,
        timestamp: this.nextTime
      })
      // console.log(res)
      res.data.data.results.forEach((item) => {
        item.pubdate = timeAgo(item.pubdate)
      })
      if (res.data.data.results.length === 0) {
        this.loading = false
        this.finished = true
      } else {
        this.nextTime = res.data.data.pre_timestamp
        this.articleList = [...this.articleList, ...res.data.data.results]
        this.loading = false
      }
    },
    onLoad () {
      if (this.articleList.length > 0) {
        this.getArticlesList()
      }
    },
    onRefresh () {
      this.nextTime = null
      this.articleList = []
      this.getArticlesList()
      this.refreshing = false
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
